package com.vf.editor.mvc.scene;

import java.util.List;

import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.kit.StrKit;
import com.vf.editor.common.controller.BaseController;
import com.vf.editor.common.model.resources.Scene;
import com.vf.editor.common.model.resources.SceneConfig;
import com.vf.editor.common.model.resources.SceneLayer;
import com.vf.editor.common.util.UuidUtil;

@Path(value = "/scene", viewPath = "/views/scene/")
public class SceneController extends BaseController{

	@Inject
	private SceneService sceneService;
	
	public void index() {
		Scene scene=Scene.dao.findById(getPara("sceneId"));
		this.set("scene", scene);
		render("index.html");
	}
	
	public void edit() {
		Scene scene=Scene.dao.findById(getPara("sceneId"));
		this.set("scene", scene);
		render("edit.html");
	}
	
	

	public void save() {
		Scene model = getModel(Scene.class);
		if (StrKit.notBlank(model.getId())) {
			if (model.update()) {
				renderSuccess("更新成功！",model);
			} else {
				renderError("更新失败！");
			}
		} else {
			model.setId(UuidUtil.getUUID());
			if (model.save()) {
				renderSuccess("保存成功！",model);
			} else {
				renderError("保存失败！");
			}
		}
	}
	
	public void findById() {
		Scene scene=Scene.dao.findById(getPara("sceneId"));
		if (scene!=null) {
			renderSuccess("更新成功！",scene);
		} else {
			renderError("数据不存在！");
		}
	}
	
	public void getResources() {
		String sceneId=this.getPara("sceneId");
		List<SceneLayer> objects=sceneService.getResources(sceneId);
		renderSuccess("成功！",objects);
	}
	
	
	public void config() {
		SceneConfig config=SceneConfig.dao.findById(getPara("sceneId"));
		this.set("config", config);
		render("config.html");
	}
	
	public void saveConfig() {
		SceneConfig model = getModel(SceneConfig.class);
		if (StrKit.notBlank(model.getId())) {
			if (model.update()) {
				renderSuccess("更新成功！",model);
			} else {
				renderError("更新失败！");
			}
		} else {
			model.setId(UuidUtil.getUUID());
			if (model.save()) {
				renderSuccess("保存成功！",model);
			} else {
				renderError("保存失败！");
			}
		}
	}
	
}
